#include "view_others_device_info.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "prj_config.h"
#include "times/view_times.h"
#include "device_type.h"
#include "font_common.h"
const unsigned char num_N310_percentage_19x32[] = {
    /* 数组大小：530 颜色数：17 图片：N411-num-percentage-24x30.bmp*/
    0x18, 0x00, 0x1e, 0x00, 0x11, 0x00, 0x00, 0x00, 0xc7, 0x39, 0x96, 0xb5, 0x9a, 0xd6, 0x14, 0xa5, 0xcf, 0x7b, 0x9e, 0xf7, 0xdf, 0xff, 0x0c, 0x63, 0x04, 0x21, 0xdb, 0xde, 0xd7, 0xbd, 0x10, 0x84,
    0x8a, 0x52, 0x59, 0xce, 0x5d, 0xef, 0x92, 0x94, 0x00, 0x81, 0x01, 0x02, 0x03, 0x81, 0x04, 0x01, 0x00, 0x86, 0x01, 0x04, 0x81, 0x01, 0x00, 0x85, 0x05, 0x06, 0x07, 0x83, 0x06, 0x08, 0x00, 0x85,
    0x02, 0x07, 0x06, 0x09, 0x00, 0x84, 0x09, 0x06, 0x07, 0x81, 0x03, 0x0a, 0x07, 0x81, 0x06, 0x09, 0x00, 0x84, 0x06, 0x07, 0x0b, 0x00, 0x85, 0x0c, 0x07, 0x81, 0x04, 0x00, 0x81, 0x04, 0x07, 0x81,
    0x0c, 0x00, 0x83, 0x05, 0x07, 0x81, 0x05, 0x00, 0x85, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x83, 0x0b, 0x07, 0x06, 0x00, 0x86, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81,
    0x08, 0x07, 0x81, 0x04, 0x00, 0x82, 0x09, 0x06, 0x07, 0x0b, 0x00, 0x86, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x82, 0x0c, 0x07, 0x81, 0x0d, 0x00, 0x86, 0x04, 0x07,
    0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x82, 0x0e, 0x07, 0x0f, 0x00, 0x87, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x81, 0x09, 0x07, 0x81, 0x02, 0x00,
    0x87, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x81, 0x0c, 0x07, 0x81, 0x01, 0x00, 0x87, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x81, 0x0a,
    0x07, 0x0a, 0x00, 0x88, 0x10, 0x07, 0x81, 0x0c, 0x00, 0x81, 0x0c, 0x07, 0x81, 0x10, 0x00, 0x01, 0x07, 0x81, 0x10, 0x00, 0x88, 0x01, 0x07, 0x81, 0x06, 0x10, 0x81, 0x06, 0x07, 0x81, 0x01, 0x00,
    0x10, 0x07, 0x81, 0x01, 0x00, 0x89, 0x02, 0x07, 0x85, 0x02, 0x00, 0x81, 0x0f, 0x07, 0x03, 0x00, 0x8b, 0x10, 0x0f, 0x07, 0x81, 0x0f, 0x0c, 0x00, 0x81, 0x0d, 0x07, 0x81, 0x0c, 0x00, 0x81, 0x01,
    0x10, 0x03, 0x81, 0x10, 0x01, 0x00, 0x8b, 0x02, 0x07, 0x81, 0x09, 0x00, 0x0d, 0x06, 0x07, 0x83, 0x06, 0x0d, 0x00, 0x8a, 0x0f, 0x07, 0x0e, 0x00, 0x09, 0x06, 0x07, 0x81, 0x0a, 0x81, 0x07, 0x81,
    0x06, 0x09, 0x00, 0x88, 0x08, 0x07, 0x81, 0x0c, 0x00, 0x0c, 0x07, 0x81, 0x0b, 0x00, 0x81, 0x0b, 0x07, 0x81, 0x05, 0x00, 0x88, 0x0b, 0x07, 0x06, 0x00, 0x81, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81,
    0x08, 0x07, 0x81, 0x04, 0x00, 0x88, 0x06, 0x07, 0x0b, 0x00, 0x81, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x87, 0x05, 0x07, 0x81, 0x05, 0x00, 0x81, 0x04, 0x07, 0x81,
    0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x87, 0x0b, 0x07, 0x0f, 0x00, 0x82, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x86, 0x09, 0x07, 0x81, 0x02, 0x00, 0x82,
    0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x86, 0x0c, 0x07, 0x81, 0x0d, 0x00, 0x82, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x86, 0x0e, 0x07,
    0x0f, 0x00, 0x83, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x85, 0x01, 0x07, 0x81, 0x04, 0x00, 0x83, 0x04, 0x07, 0x81, 0x05, 0x00, 0x81, 0x05, 0x07, 0x81, 0x04, 0x00,
    0x85, 0x10, 0x07, 0x81, 0x01, 0x00, 0x83, 0x0d, 0x07, 0x81, 0x0a, 0x05, 0x81, 0x0a, 0x07, 0x81, 0x0d, 0x00, 0x85, 0x0a, 0x07, 0x0a, 0x00, 0x85, 0x0b, 0x07, 0x85, 0x0b, 0x00, 0x85, 0x01, 0x07,
    0x81, 0x10, 0x00, 0x85, 0x09, 0x02, 0x07, 0x83, 0x02, 0x09, 0x00, 0x91, 0x09, 0x08, 0x81, 0x09, 0x00, 0x82,
};

void view_others_device_info_display(void)
{
    int x = 76;
    int y = 42;
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[1] = battery_ctrl_if_charging();
    }
    uint8_t battery_level = battery_ctrl_get_level(disp_ctrl.com_data.i_temp[0]);
    if (disp_ctrl.com_data.i_temp[1])
    {
        disp_ctrl.auto_refresh_time = 500;

        battery_level = disp_ctrl.auto_refresh_count % 5;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_F301_version_ele_0_80_42 + battery_level);
    }
    else
    {
        disp_ctrl.auto_refresh_time = 5000;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_F301_version_ele_0_80_42 + battery_level);
    }
    y = 91;
    int const num_width = 15;
    int width = 19 + get_num_digit(disp_ctrl.com_data.i_temp[0]) * num_width;
    // x = 120 - width / 2;
    x = disp_draw_user_image_num_mid_ignore(120, y, RGB565_COLOR_BLACK, disp_ctrl.com_data.i_temp[0], num_width, num_12x29_TAB);
    disp_draw_user_image(x, y - 2, num_N310_percentage_19x32);

    char buffer[20];
    uint16_t len = 16;
    memset(buffer, 0, sizeof(buffer));
    sd_ble_gap_device_name_get((uint8_t *)buffer, &len);
    // disp_show_font_string_utf8(60, 183, buffer, 0);
    disp_show_user_string_middle(0, 240, 155, buffer, num_hex_12x28_get_bmp);
}

void view_others_device_info_detail_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_close_delay = 10000;
        disp_ctrl.auto_refresh_time = 500;
        memset(&disp_ctrl.com_data.uint8_temp[100], 0, 64);
        get_show_device_name(&disp_ctrl.com_data.uint8_temp[100], 32);
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
    }

    int x = 83;
    int y = 10;
    if (disp_ctrl.com_data.i_temp[0])
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_connect);
    }
    else
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_disconnect);
    }

    int const device_y_interval = 55;
    // int const name_start_y = 20;
    int const hw_start_y = device_y_interval;
    int const fw_start_y = hw_start_y + device_y_interval;
    int const mac_start_y = fw_start_y + device_y_interval;

    uint8_t hardware[32] = {0};
    uint8_t firmware[32] = {0};
    strcpy((char *)hardware, (char *)&disp_ctrl.com_data.uint8_temp[100]);
    sprintf((char *)hardware, "%s_V%s", hardware, HARDWARE_VERSION);
    sprintf((char *)firmware, "%s_%s", FIRMWARE_VERSION, FW_STRING_DATE);

    //硬件版本号
    disp_show_user_string_middle(0, 240, hw_start_y, (char *)hardware, num_hex_12x28_get_bmp);

    //固件版本号
    disp_show_user_string_middle(0, 240, fw_start_y, (char *)firmware, num_hex_12x28_get_bmp);

    //蓝牙地址
    char buffer[32] = {0};
    memset(buffer, 0, sizeof(buffer));
    ble_gap_addr_t p_addr;
    sd_ble_gap_address_get(&p_addr);
    sprintf(buffer, "%1X%1X:%1X%1X:%1X%1X:%1X%1X:%1X%1X:%1X%1X", p_addr.addr[5] >> 4, p_addr.addr[5] & 0xf, p_addr.addr[4] >> 4, p_addr.addr[4] & 0xf, p_addr.addr[3] >> 4, p_addr.addr[3] & 0xf,
            p_addr.addr[2] >> 4, p_addr.addr[2] & 0xf, p_addr.addr[1] >> 4, p_addr.addr[1] & 0xf, p_addr.addr[0] >> 4, p_addr.addr[0] & 0xf);

    disp_show_user_string_middle(0, 240, mac_start_y, buffer, num_hex_9x19_get_bmp); //总长度7+16+14=37*6-7=215
}
